/**
 * Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
 *
 * @Package: com.github.mybatis.fl.util
 * @author: flying-cattle
 * @date: 2019年4月9日 下午8:15:25
 */
package com.github.flying.cattle.mdg.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.github.flying.cattle.mdg.entity.BasisInfo;
import com.github.flying.cattle.mdg.entity.PropertyInfo;

/**
 * Copyright: Copyright (c) 2019
 *
 * <p>说明：  链接数据库并获取表信息</P>
 *
 * @version: v3.0.0
 * @author: flying-cattle
 * <p>
 * Modification History:
 * Date         	Author          Version          Description
 * ---------------------------------------------------------------*
 * 2019年4月9日      		flying-cattle   v3.0.0           initialize
 */
public class EntityInfoUtil {

    public static BasisInfo getInfo(BasisInfo bi) throws SQLException {
        return getInfo(bi, null);
    }

    private static boolean checkIgnore(String column, String[] ignoreColumn) {
        if (ignoreColumn == null || column == null) return false;
        for (String tmp : ignoreColumn) {
            if (tmp != null && tmp.toLowerCase().equals(column.toLowerCase())) {
                return true;
            }
        }
        return false;
    }

    public static BasisInfo getInfo(BasisInfo bi, String[] ignoreColumn) throws SQLException {
        List<PropertyInfo> columns = new ArrayList<PropertyInfo>();
        // 创建连接
        Connection con = null;
        PreparedStatement pstemt = null;
        ResultSet rs = null;
        //sql
        String sql = "select column_name,data_type,column_comment,column_key,character_maximum_length from information_schema.columns where table_schema='" + bi.getDatabase() + "' and table_name='" + bi.getTable() + "'";
        String tableCommentSql = "select TABLE_COMMENT from information_schema.TABLES where table_schema='" + bi.getDatabase() + "' and table_name='" + bi.getTable() + "'";
        try {
            con = DriverManager.getConnection(bi.getDbUrl(), bi.getDbName(), bi.getDbPassword());
            pstemt = con.prepareStatement(sql);
            rs = pstemt.executeQuery();
            PreparedStatement comment_pstemt = con.prepareStatement(tableCommentSql);
            ResultSet commentSet = comment_pstemt.executeQuery();
            commentSet.next();
            String tabComment = commentSet.getString(1);
            bi.setTableComment(tabComment);
            while (rs.next()) {
                String column = rs.getString(1);
                if (checkIgnore(column, ignoreColumn)) continue;
                String jdbcType = rs.getString(2).toUpperCase();
                String comment = rs.getString(3);
                String columnKey = rs.getString(4);
                Long maxLength = rs.getLong(5);
                PropertyInfo ci = new PropertyInfo();
                ci.setColumn(column);
                if (jdbcType.equalsIgnoreCase("int")) {
                    ci.setJdbcType("Integer".toUpperCase());
                } else if (jdbcType.equalsIgnoreCase("datetime")) {
                    ci.setJdbcType("timestamp".toUpperCase());
                } else {
                    ci.setJdbcType(jdbcType);
                }
                ci.setComment(comment);
                ci.setMaxLength(maxLength);
                ci.setProperty(MySqlToJavaUtil.changeToJavaFiled(column));
                ci.setJavaType(MySqlToJavaUtil.jdbcTypeToJavaType(jdbcType));
                if (columnKey != null) {
                    ci.setColumnKey(columnKey.toLowerCase());
                }
                //设置注解类型
                if (column.equalsIgnoreCase("id") || "pri".equalsIgnoreCase(columnKey)) {
                    bi.setIdType(ci.getJavaType());
                    bi.setIdJdbcType(ci.getJdbcType());
                }
                columns.add(ci);
                //添加包路径
                Set<String> pkgs = bi.getPkgs();
                pkgs.add(MySqlToJavaUtil.jdbcTypeToJavaTypePck(jdbcType));
                bi.setPkgs(pkgs);
            }
            bi.setCis(columns);
            // 完成后关闭
            rs.close();
            pstemt.close();
            comment_pstemt.close();
            con.close();
            if (null == columns || columns.size() == 0) {
                throw new RuntimeException("未能读取到表或表中的字段。请检查链接url，数据库账户，数据库密码，查询的数据名、是否正确。");
            }
            return bi;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("自动生成实体类错误：" + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
            } catch (SQLException se2) {
            }
            // 关闭资源
            try {
                if (pstemt != null) pstemt.close();
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (con != null) con.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }
}
